package com.bincloud.vue.admin.web.controller.api;

import com.alibaba.fastjson.JSON;
import com.bincloud.vue.admin.common.model.dto.response.BaseResDTO;
import com.bincloud.vue.admin.common.util.ResponseUtils;
import com.bincloud.vue.admin.web.dto.api.auth.AuthLoginReqDTO;
import com.bincloud.vue.admin.web.dto.api.auth.AuthResDTO;
import com.bincloud.vue.admin.web.dto.api.auth.AuthValidateReqDTO;
import com.bincloud.vue.admin.web.service.api.AuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "权限相关接口")
@RestController
@RequestMapping(value = "/api/auth")
@Slf4j
public class AuthController {
    @Resource
    private AuthService authService;

    @ApiOperation(value = "用户登录", produces = "application/json")
    @PostMapping(value = "/login")
    public BaseResDTO<AuthResDTO> login(@Validated @RequestBody AuthLoginReqDTO reqDTO) {
        log.info("用户登录,入参:{}", JSON.toJSONString(reqDTO));
        AuthResDTO resDTO = authService.loginUser(reqDTO);
        log.info("用户登录,返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok(resDTO);
    }

    @ApiOperation(value = "用户登录(一次验证)", produces = "application/json")
    @PostMapping(value = "/twoFactorLogin")
    public BaseResDTO<AuthResDTO> twoFactorLogin(@Validated @RequestBody AuthLoginReqDTO reqDTO) {
        log.info("用户登录(一次验证),入参:{}", JSON.toJSONString(reqDTO));
        AuthResDTO resDTO = authService.twoFactorLogin(reqDTO);
        log.info("用户登录(一次验证),返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok(resDTO);
    }

    @ApiOperation(value = "用户登录(二次验证)", produces = "application/json")
    @PostMapping(value = "/validateLogin")
    public BaseResDTO<AuthResDTO> validateLogin(@Validated @RequestBody AuthValidateReqDTO reqDTO) {
        log.info("用户登录(二次验证),入参:{}", JSON.toJSONString(reqDTO));
        AuthResDTO resDTO = authService.validateLogin(reqDTO);
        log.info("用户登录(二次验证),返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok(resDTO);
    }

}
